package com.srwl.mytx.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.srwl.mytx.R;
import com.srwl.mytx.adapter.BottomTopicListAdapter;
import com.srwl.mytx.domain.Topic;

import java.util.List;

import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;

public class BottomTopicListV1 extends LinearLayout {
    private static final String TAG = "BottomTopicList";
    private InteractionListener listener;
    private ViewPager vp_dialogItems;
    public BottomTopicListAdapter bottomTopicAdapter;
    private Context context;
    private BottomSheetBehavior<View> bottomSheetBehavior;

    public BottomTopicListV1(@NonNull Context context) {
        super(context);
        this.context = context;
        init();
    }


    public BottomTopicListV1(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public BottomTopicListV1(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    public BottomTopicListV1(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.context = context;
    }

    private void init() {
        inflate(context, R.layout.bottom_dialog_v1, this);
        vp_dialogItems = findViewById(R.id.vp_dialog);
        vp_dialogItems.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                if (listener != null) {
                    listener.onPageChange(position);
                }
//                if (bottomTopicAdapter.beforeVideoPlayer != null && bottomTopicAdapter.beforeVideoPlayer.state == Jzvd.STATE_PLAYING) {
//                    bottomTopicAdapter.beforeVideoPlayer.pauseVideo();
//                    Log.i(TAG, "已暂停");
//                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    public void init(List<Topic> topicList) {
        bottomTopicAdapter = new BottomTopicListAdapter(context, topicList);
        vp_dialogItems.setAdapter(bottomTopicAdapter);
    }

    public void setBottomSheetBehavior(BottomSheetBehavior<View> bottomSheetBehavior) {
        this.bottomSheetBehavior = bottomSheetBehavior;
        bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View view, int newState) {
                Log.d(TAG, "onStateChanged: " + newState);
                if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                    setVisibility(GONE);
                    if (listener != null) {
                        listener.onHide();
                        if (bottomTopicAdapter.getVideoPlayer() != null && bottomTopicAdapter.getVideoPlayer().state == Jzvd.STATE_PLAYING) {
                            JzvdStd.resetAllVideos();
                        }
                    }
                }
            }

            @Override
            public void onSlide(@NonNull View view, float slideOffset) {
                Log.d(TAG, "onSlide: " + slideOffset + "view.getTop" + view.getTop());

                float scrollY = ((1 + slideOffset) * view.getHeight());
                if (listener != null && !Float.isNaN(scrollY)) {
                    listener.onScrolled(scrollY);
                }
            }
        });
    }

    public void init(int position) {
        vp_dialogItems.setCurrentItem(position);
    }

    public void setListener(InteractionListener listener) {
        this.listener = listener;
    }

    public void show(int position) {
        setVisibility(VISIBLE);
        bottomTopicAdapter.notifyDataSetChanged();
        vp_dialogItems.setCurrentItem(position, false);
        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
        bottomSheetBehavior.setSkipCollapsed(false);
    }

    public boolean hide() {
        if (bottomSheetBehavior == null) {
            return false;
        }
        if (bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_COLLAPSED || bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
            bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
            setVisibility(GONE);
            return true;
        }
        return false;
    }

    public void notifyDataSetChange() {
        bottomTopicAdapter.notifyDataSetChanged();
    }

    public void notifyItemChange(int position) {
        bottomTopicAdapter.notifyDataSetChanged();
    }

    public void notifyItemRemove(int position) {
        bottomTopicAdapter.notifyDataSetChanged();

    }

    public interface InteractionListener {
        void onPageChange(int position);

        /**
         * @param dy Y方向滑动的距离
         */
        void onScrolled(float dy);

        void onHide();
    }
}
